package com.study.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @description 锁类
 * @author hw
 * @date 2022/1/6 21:16:29
 */
public class SyncClassThread extends Thread{
    Logger logger = LoggerFactory.getLogger(SyncClassThread.class);

    @Override
    public void run() {
        this.test1();
    }

    /**
     * @description 同步代码块传参class对象（全局锁）
     * 所有调用该方法的线程都会实现同步
     * @author hw
     * @date 2022/1/6 21:24:27
     */
    public void test1(){
        synchronized (SyncClassThread.class){
            try {
                logger.info(Thread.currentThread().getName() + "run 进入了同步块");
                Thread.sleep(1000);
                logger.info(Thread.currentThread().getName() + "run 休眠结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
